﻿Public Class frmMainLogin

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Public loginHash As String
    Public uid As String
    Private LoggingIn As Boolean
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        loginBrowser.Navigate("http://www.smartpromoter.net/sp/bb.php?a=" & UsernameTextBox.Text & "&p=" & PasswordTextBox.Text)
        LoggingIn = True
        OK.Text = "Logging in..."
        'Me.Close()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

        'stop logging in

        loginBrowser.Stop()
        tmrLogin.Stop()
        LoggingIn = False
        tmrLogin.Enabled = False
        OK.Text = "Log In"

    End Sub

    Private Sub frmMainLogin_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    End Sub

    Private Sub frmMainLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        On Error Resume Next

        LoggingIn = False

        Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\main_settings.dat")
        Dim strData

        strData = Split(objReader.ReadLine(), "|")
        objReader.Close()

        If My.Settings.saveMainCredentials = True Then

            chkSaveInfo.Checked = True
            UsernameTextBox.Text = My.Settings.username
            PasswordTextBox.Text = DeHex(My.Settings.password)

        End If

        PasswordTextBox.Focus()

        If My.Settings.autoMainLogin = True Then

            chkAutoLogin.Checked = True
            UsernameTextBox.Text = My.Settings.username
            PasswordTextBox.Text = DeHex(My.Settings.password)

            tmrLogin.Enabled = True
            OK.Text = "Logging in..."

        End If

    End Sub

    Private Sub loginBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles loginBrowser.DocumentCompleted

        If LoggingIn = True Then
            Dim strData As String
            strData = loginBrowser.DocumentText
            If InStr(strData, "WORDUP") Then
                Dim strSplitData() As String
                strSplitData = Split(strData, "|")
                loginHash = strSplitData(1)
                uid = strSplitData(2)

                oWrite = IO.File.CreateText(Application.StartupPath & "\main_settings.dat")
                oWrite.WriteLine(uid + "|" + loginHash + "|" + UsernameTextBox.Text)
                oWrite.Close()

                My.Settings.username = UsernameTextBox.Text
                My.Settings.password = EnHex(PasswordTextBox.Text)

                Me.Hide()
                frmLogin.Show()

            ElseIf InStr(strData, "424144") Then
                If InStr(1, strData, "INVALID", vbTextCompare) Then

                    MsgBox("Insufficient information was supplied or username/password were incorrect. Please Try Again.", vbCritical, "Login Failed")

                ElseIf InStr(1, strData, "INACTIVE", vbTextCompare) Then

                    MsgBox("Your account is inactive. Please visit your account area online for more information.", vbCritical, "Login Failed")

                ElseIf InStr(1, strData, "NOPASSWORDSET", vbTextCompare) Then

                    MsgBox("Your account hasn't set a program password. Please visit the forums and set your program password in your user control panel.", vbCritical, "Login Failed")

                End If
            End If
        Else

            MsgBox("Login Failed. Try again.", vbCritical, "Login Failed")

        End If

        LoggingIn = False

    End Sub

    Private Sub chkSaveInfo_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkSaveInfo.CheckedChanged

        My.Settings.saveMainCredentials = chkSaveInfo.Checked
        My.Settings.Save()

    End Sub

    Private Sub chkAutoLogin_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAutoLogin.CheckedChanged

        chkSaveInfo.Checked = True
        My.Settings.saveMainCredentials = chkSaveInfo.Checked
        My.Settings.autoMainLogin = chkAutoLogin.Checked
        My.Settings.Save()

    End Sub

    Private Sub tmrLogin_Tick(sender As System.Object, e As System.EventArgs) Handles tmrLogin.Tick

        OK.PerformClick()
        tmrLogin.Enabled = False

    End Sub
End Class
